#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>> m(numCourses);
        vector<int> in(numCourses);
        for (auto e : prerequisites)
        {
            int cur = e[0];
            int prev = e[1];
            m[prev].push_back(cur);
            in[cur]++;
        }

        queue<int> q;
        for (int i = 0; i < numCourses; i++)
            if (in[i] == 0) q.push(i);

        vector<int> ret;
        while (q.size())
        {
            int cur = q.front(); q.pop();
            ret.push_back(cur);
            for (auto e : m[cur])
            {
                in[e]--;
                if (in[e] == 0) q.push(e);
            }
        }

        return ret.size() == numCourses ? ret : vector<int>();
    }
};